package com.huijie.core.util;

import com.huijie.core.bean.ResponseBean;
import com.huijie.core.bean.ResponseCodeMsgBean;
import com.huijie.core.constant.BussinessCode;
import com.huijie.core.constant.ResponseCode;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * @ClassName ResponseMsgUtil
 * @Description Restful Api 系统返回码工具类
 * @Author Alex
 * @Date 2018/9/20 19:45
 * @Version 1.0
 */
public class ResponseMsgUtil {

    /**
     * 当系统未定义的错误被捕捉时返回码
     */
    public static final int ERROR_CODE_SYSTEM = 900;

    /**
     * 请求参数系统验证不通过返回码
     */
    public static final int VALIDATOR_CODE_SYSTEM = 800;

    /**
     * @param responseCode 系统返回码和对应返回描述枚举
     * @return com.cn.rfid.model.vo.base.ResponseCodeMsgBean
     * @description: 返回消息代码code 和 消息描述message
     * @author Alex
     * @date 2018/9/21 8:52
     */
    public static ResponseCodeMsgBean returnCodeMessage(ResponseCode responseCode) {
        return new ResponseCodeMsgBean(responseCode.getCode(), responseCode.getMessage());
    }

    /**
     * @param responseCode 系统返回码和对应返回描述枚举
     * @param returnData   返回数据
     * @return com.cn.rfid.model.vo.base.ResponseBean
     * @description: 返回消息代码code 和 消息描述message 和 消息对象data
     * @author Alex
     * @date 2018/9/21 9:21
     */
    public static ResponseBean returnCodeMessage(ResponseCode responseCode, Object returnData) {
        return new ResponseBean(responseCode.getCode(), responseCode.getMessage(), returnData);
    }

    /**
     * @param businessCode 业务返回码和对应返回描述枚举
     * @param returnData   返回数据
     * @return com.cn.rfid.model.vo.base.ResponseBean
     * @description: 返回消息代码code 和 消息描述message 和 消息对象data
     * @author Alex
     * @date 2018/9/21 9:21
     */
    public static ResponseBean returnCodeMessage(BussinessCode businessCode, Object returnData) {
        return new ResponseBean(businessCode.getCode(), businessCode.getMessage(), returnData);
    }

//    /**
//     * @param businessCode 业务返回码和对应返回描述枚举
//     * @param returnData 返回数据
//     * @return com.cn.rfid.model.vo.base.ResponseBean
//     * @description: 返回消息代码code 和 消息描述message 和 消息对象data
//     * @author Alex
//     * @date 2018/9/21 9:21
//     */
//    public static ResponseBean returnCodeMessage(BussinessCode businessCode) {
//        return new ResponseBean(businessCode.getCode(),businessCode.getMessage());
//    }

    /**
     * @param businessCode 业务返回码和对应返回描述枚举
     * @return com.cn.rfid.model.vo.base.ResponseBean
     * @description: 返回消息代码code 和 消息描述message
     * @author Alex
     * @date 2018/11/28 11:22
     */
    public static ResponseBean returnCodeMessage(BussinessCode businessCode) {
        return new ResponseBean(businessCode.getCode(), businessCode.getMessage());
    }

    /**
     * @param businessCode 业务返回码和对应返回描述枚举
     * @return com.cn.rfid.model.vo.base.ResponseCodeMsgBean
     * @description: 业务返回码和对应返回描述枚举
     * @author Alex
     * @date 2018/11/28 11:23
     */
    public static ResponseCodeMsgBean returnCodeMessageBean(BussinessCode businessCode) {
        return new ResponseCodeMsgBean(businessCode.getCode(), businessCode.getMessage());
    }

    /**
     * @param responseBean  Restful Api 返回实体
     * @param bindingResult 校验结果
     * @return com.cn.rfid.model.vo.base.ResponseBean
     * @description: GET返回参数校验消息代码和数据
     * @author Alex
     * @date 2018/9/21 10:10
     */
    public static ResponseBean returnCodeMessage(ResponseBean responseBean, BindingResult bindingResult) {
        StringBuffer sf = getValidatorErrors(bindingResult);
        responseBean.setCode(VALIDATOR_CODE_SYSTEM);
        responseBean.setMessage(sf.toString());
        return responseBean;
    }

    /**
     * @param responseCodeMsgBean Restful Api 返回实体
     * @param bindingResult       校验结果
     * @return com.cn.rfid.model.vo.base.ResponseCodeMsgBean
     * @description: POST返回参数校验消息代码和数据
     * @author Alex
     * @date 2018/9/21 10:10
     */
    public static ResponseCodeMsgBean returnCodeMessage(ResponseCodeMsgBean responseCodeMsgBean, BindingResult bindingResult) {
        StringBuffer sf = getValidatorErrors(bindingResult);
        responseCodeMsgBean.setCode(VALIDATOR_CODE_SYSTEM);
        responseCodeMsgBean.setMessage(sf.toString());
        return responseCodeMsgBean;
    }

    private static StringBuffer getValidatorErrors(BindingResult bindingResult) {
        StringBuffer sf = new StringBuffer();
        if (null != bindingResult.getGlobalErrors() && bindingResult.getGlobalErrors().size() > 0) {
            bindingResult.getGlobalErrors().forEach(error -> sf.append(error.getObjectName()).
                    append(":").append(error.getDefaultMessage()).append("; "));
        } else {
            bindingResult.getAllErrors().forEach(error -> sf.append(((FieldError) error).getField()).
                    append(":").append(error.getDefaultMessage()).append("; "));
        }
        return sf;
    }
}
